<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="编号:" prop="id">
          <el-input v-model="queryParams.id" placeholder="请输入编号" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="订单类型:" prop="type">
          <el-select v-model="queryParams.type" clearable placeholder="请选择">
            <el-option label="买多" value="BUY_MORE" />
            <el-option label="买空" value="BUY_SHORT" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态:" prop="positionStatus">
          <el-select v-model="queryParams.positionStatus" clearable placeholder="请选择">
            <el-option label="持仓中" value="IN_POSITION" />
            <el-option label="平仓" value="CLOSING_POSITION" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="交易时间:" prop="date">
          <el-date-picker style="width: 280px" v-model="queryParams.date" type="daterange" clearable placeholder="日期区间选择"
            format="YYYY-MM-DD" value-format="YYYY-MM-DD" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      
      <el-table v-loading="loading" :data="walletMarketList" @selection-change="handleSelectionChange">
        <el-table-column label="id" align="center" prop="userId" show-overflow-tooltip />
        <el-table-column label="金额" align="center" prop="amount" />
        <el-table-column label="类型" align="center" prop="type">
          <template #default="scope">
            <el-tag type="success" effect="plain" v-if="scope.row.type == '11'">
              充值
            </el-tag>
            <el-tag type="danger" effect="plain" v-if="scope.row.type == '12'">
              提现
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="通道" align="center" prop="channel" />
        <el-table-column label="钱包地址" align="center" prop="cashAddress" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag type="info" effect="dark" v-if="scope.row.status == '0'">
              初始
            </el-tag>
            <el-tag type="success" effect="dark" v-if="scope.row.status == '1'">
              完成
            </el-tag>
            <el-tag type="danger" effect="dark" v-if="scope.row.status == '2'">
              失败
            </el-tag>
            <el-tag type="warning" effect="dark" v-if="scope.row.status == '3'">
              处理中
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="充值凭证" align="center" prop="imgUrl" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="90">
          <template #default="scope">
            <div v-if="scope.row.status == '0'">
              <el-button link size="small" type="primary" @click="handleConfirm(scope.row)"
                v-hasPermi="['system:walletRechargeCash:confirm']">通过
              </el-button>
              <el-button link size="small" type="danger" @click="handleRefuse(scope.row)"
                v-hasPermi="['system:walletRechargeCash:refuse']">驳回
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size"
        @pagination="getList" />
      <!-- 驳回订单填入原因对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="walletRechargeCashRef" :model="refuseForm" label-width="80px">
          <el-form-item label="驳回原因" prop="refuseReason" required>
            <el-input v-model="refuseForm.refuseReason" placeholder="请输入驳回原因" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitRefuseForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="WalletMarket">
  import { walletRechargeList,rechargeConfirm,rechargeRefuse,gameUpList } from "@/api/charge/index.js";
  
  const { proxy } = getCurrentInstance();
  
  const walletMarketList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      current: 1,
      size: 10,
      type: null,
      status: null,
      positionStatus: null,
      startTime: null,
      endTime: null,
    },
    rules: {}
  });
  const refuseForm = ref({
    refuseReason: '',
    id: null
  })
  
  const { queryParams, form, rules } = toRefs(data);
  
  /* */
  function getList() {
    loading.value = true;
    gameUpList(queryParams.value).then(res => {
      walletMarketList.value = res.data.records;
      total.value = res.data.total;
      loading.value = false;
    })
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      id: null,
      type: null,
      channel: null,
      amount: null,
      balance: null,
      duration: null,
      status: null,
      positionStatus: null,
      startTime: null,
      startPrice: null,
      endTime: null,
      endPrice: null,
      lossOrGain: null,
      userNo: null
    };
    proxy.resetForm("walletMarketRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.current = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  function handleConfirm(row) {
    proxy.$modal.confirm_tg('是否确认【' + row.type + '】订单"' + row.id + '"完成', '确认订单').then(function () {
      rechargeConfirm(row.id).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess("操作成功");
          getList();
        }
      })
    }).catch(() => { })
  }
  
  function handleRefuse(row) {
    reset(row);
    refuseForm.value.id = row.id
    title.value = '驳回'
    open.value = true;
  }
  
  function submitRefuseForm() {
    rechargeRefuse(refuseForm.value).then(res => {
      if (res.code === 200) {
        proxy.$modal.msgSuccess("驳回成功");
        refuseForm.value.id = null
        open.value = false;
        getList();
      } else {
        
      }
    })
  }
  
  getList();
  // loading.value = false;
  </script>
  